package controller;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import domain.PageBean;
import domain.User;
import service.UserService;
import service.impl.UserServiceImpl;
import utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/userservlet")
public class UserServlet extends BaseServlet {
    ///userservlet?method=userLogin
    public String userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1获取前端数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //校验
        if (StringUtils.isEmpty(username)) {
            request.setAttribute("msg", "用户名不能为空");
            return "/message.jsp";
        }
        if (StringUtils.isEmpty(password)) {
            request.setAttribute("msg", "密码不能为空");
            return "/message.jsp";
        }


        try {
            //调用业务逻辑
            UserService userService = new UserServiceImpl();
            User user = userService.login(username, password);
            if (user != null) {
                request.getSession().setAttribute("user", user);
                if (user.getStatus() == 1) {
                    return "/adminHome.jsp";
                } else {
                    return "/studentHome.jsp";
                }
            } else {
                request.setAttribute("msg", "用户名或密码错误");
                return "redirect:/login.jsp";

            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "登录失败：" + e.getMessage());
            return "/message.jsp";
        }
    }
    ///userservlet?method=viewStudents

    public String viewStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        UserService userService = new UserServiceImpl();
        List<User> stuList = userService.findAll();
        request.setAttribute("stuList", stuList);
        System.out.println(stuList);

        return "/viewStudents.jsp";
    }

    ///userservlet?method=deleteStudents
    public String deleteStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端数据
        String id = request.getParameter("id");
        System.out.println("id = " + id);
        //非空验证
        if (StringUtils.isEmpty(id)) {
            return "redirect:/viewStudents.jsp";
        }
        try {
            //2调用业务逻辑
            UserService userService = new UserServiceImpl();
            int bid = Integer.parseInt(id);
            userService.remove(bid);
            //转发重定向
            return "redirect:/userservlet?method=viewStudents";
        } catch (NumberFormatException e) {
            e.printStackTrace();
            request.setAttribute("msg", "删除失败");
            return "/message.jsp";
        }
    }

    ///userservlet?method=addStudents
    public String addStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        // 获取前端数据
        String stuname = request.getParameter("stuname");
        String stuno = request.getParameter("stuno");
        String pwd = request.getParameter("pwd");
        String idcard = request.getParameter("idcard");
        String startdateStr = request.getParameter("startdate");
        String statusStr = request.getParameter("status");
        try {
            // 转换日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startdate = null;
            int status = 0;
            try {
                startdate = sdf.parse(startdateStr);
                status = Integer.parseInt(statusStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }


            User user = new User(0, stuname, stuno, pwd, idcard, startdate, status);

            // 调用业务逻辑添加学生
            UserService userService = new UserServiceImpl();
            userService.addStudent(user);

            // 添加成功后重定向
            return "redirect:/userservlet?method=viewStudents";
        } catch (Exception e) {
            e.printStackTrace();

            request.setAttribute("msg", "添加失败");
            return "/message.jsp";
        }
    }

    ///userservlet?method=updateStudents
    public String updateStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取前端数据
        int id = Integer.parseInt(request.getParameter("id"));
        String stuname = request.getParameter("stuname");
        String pwd = request.getParameter("pwd");
        String idcard = request.getParameter("idcard");
        String startDateStr = request.getParameter("startdate");
        String stuno = request.getParameter("stuno");
        try {
            // 解析日期
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = sdf.parse(startDateStr);

            // 构建学生对象
            User user = new User(id, stuname, stuno, pwd, idcard, startDate, 0); // 假设学号和权限在页面上不修改

            // 调用业务逻辑修改学生信息
            UserService studentService = new UserServiceImpl();
            studentService.updateStudent(user);

            // 修改成功后重定向
            return "/userservlet?method=selectStudents";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "修改个人信息失败：" + e.getMessage());
            return "/message.jsp";
        }
    }

    ///userservlet?method=selectStudents
    public String selectStudents(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserDao userDao = new UserDaoImpl();
        int id = Integer.parseInt(request.getParameter("id"));
       User user =  userDao.selectStudents(id);
        System.out.println(user);
        request.getSession().setAttribute("user",user);
        return "/stuinfo.jsp";
    }
}
